/*
  Copyright (C) 2011, Kyungjoo Kim
  
  This file is part of UHM (Unassembled HyperMatrix) sparse direct solver.
  
  All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  Also add information on how to contact you by electronic and paper mail.
  
  
  Kyungjoo Kim
  iamkyungjoo@gmail.com
*/
#ifndef UHM_SCHEDULER_HXX
#define UHM_SCHEDULER_HXX

namespace uhm {
  typedef class Node_*      Node;
  typedef class Element_*   Element;
  typedef class Mesh_*      Mesh;
  typedef class Scheduler_* Scheduler;

  bool scheduler_valid(Scheduler s);

  // ----------------------------------------------------------------
  // ** Scheduler class
  class Scheduler_: public Object_<int> {
  protected:
    Element root;
    std::map    < int, std::vector< Element > > elements;
    std::vector < Element >                     leaves;

    void _init(int);
  public:
    Scheduler_();
    Scheduler_(int id);
    virtual ~Scheduler_();
    virtual bool disp();
    virtual bool disp(int datasize, int flop);
    virtual bool disp(FILE *stream, int datasize, int flop);

    void load(Mesh m);
    void unload();
        
    int  get_n_elements();
    int  get_n_leaves();

    int  is_loaded();

    Element get_root();
    void get_orphan(std::vector<Element>& orphan);

    bool execute_tree(bool (*op_func)(Element), int is_leaf2root);
    bool execute_elements_seq(bool (*op_func)(Element), int is_leaf2root);
    bool execute_elements_par(bool (*op_func)(Element), int is_leaf2root);
    bool execute_leaves_seq(bool (*op_func)(Element));
    bool execute_leaves_par(bool (*op_func)(Element));


    // following is not implemented yet : actually it is not necessary
    bool execute_elements(bool (*op_func_1)(Element), 
			  bool (*op_func_2)(Element),
			  int is_leaf2root);
  
    // friends
    friend bool scheduler_valid(Scheduler s);
  };
  // ----------------------------------------------------------------
  // ** Definition
  inline void Scheduler_::_init(int) {
    this->cookie = UHM_SCHEDULER_COOKIE;
    this->id = id;
    this->root = NULL;
  }
  inline bool scheduler_valid(Scheduler s) { 
    return (s && s->cookie == UHM_SCHEDULER_COOKIE);
  }
}


#endif
